var id
var dataParm
var app = getApp();
Page({
  data: {
    maxPage: 10, //每页显示最大行数
    startPoint: 1, //第几页
    totalPages: 1, //总共几页
    page: 1,
    showloading: true,
    showbottom: true,
    list: [],
    authbg: getApp().globalData.authImg,
    authbtn2: getApp().globalData.source + "red_packet/btn2.png",
    authdialog: getApp().globalData.source + "dialog/dialog.png",
    dingshihongbaoshow: false,
    dingshihongbao: getApp().globalData.source + "dialog/dingshihongbao.png",
  },
  bindGetUserInfo: function(e) {
    getApp().authInfo(e);
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onShow:function(){

  },
  onLoad: function(options) {
    if (this.checklogin()) return
    id = options.id;
    var title = options.classify;
    wx.setNavigationBarTitle({
      title: title,
    })
    this.getdata()
    var showhomeicon = options.showhomeicon == "fx" ? true : ""; //判断是分享页进入还是小程序正常进入
    if (showhomeicon) {
      this.setData({
        showhomeicon: showhomeicon
      })
    }
  },


  checklogin: function() {
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
      if (wx.getStorageSync("authtype") == "0") {
        this.setData({
          showAuth: true,
          showAuth2: false
        })
      } else {
        this.setData({
          showAuth: false,
          showAuth2: true
        })
      }
      return true
    } else {
      this.setData({
        showAuth: false,
        showAuth2: false
      })
      return false
    }
  },

  // 回到首页
  gohome: function() {
    wx.switchTab({
      url: '/pages/style/style',
    })
  },
  getdata: function() {
    var that = this;
    var url
    var usersIds = wx.getStorageSync("userId") == "" ? -1 : wx.getStorageSync("userId")
    dataParm = {
      categoryId: id,
      startPoint: that.data.page,
      maxPage: that.data.maxPage,
      userId: usersIds
    }
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
      url = "programResource/program/" + id //统一节目列表
      //调用查询数据列表方法
    } else {
      url = "programResource/program/" + id //统一节目列表 
    }
    that.setData({
      url: url
    })
    that.getDataList(url, dataParm);
  },

  /**
   * 获取栏目数据列表
   */
  getDataList: function(url, dataParm) {
    var that = this;
    app.request(app.globalData.https + url,
      "get", dataParm,
      function(res) {
        wx.stopPullDownRefresh()
        if (res.code == 0) {
          var dt = res.data;
          console.log(dt)
          if (res.data.data.length > 0) {
            // 获取系统信息  
            that.setData({
              list: that.data.list.concat(res.data.data),
              totalPages: dt.totalPages
            });
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
              duration: 3000
            })
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 3000
          })
        }

        that.setData({
          showloading: false
        })
      }
    )
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      page: 1,
      list: []
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var url = that.data.url;
    if (that.data.page < that.data.totalPages) {
      that.setData({
        bottomtext: '正在加载中...',
        page: that.data.page + 1
      })
      var userids = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
      console.log(userids)
      dataParm = {
        userId: userids,
        categoryId: id,
        startPoint: that.data.page,
        maxPage: that.data.maxPage
      }
      that.getDataList(url, dataParm);
    } else {
      that.setData({
        bottomtext: '没有更多内容了'
      })
    }
  },


  /**
   * 跳转到播放详情页面
   */
  todetails: function(e) {
    let audioList = this.data.list
    var indexNumbers = e.currentTarget.dataset.index; //当前操作对象的 index\
    var season = e.currentTarget.dataset.season;
    if (season == undefined) {
      season = 0;
    }
    var id = audioList[indexNumbers].id;
    // 是否启用h5 
    var h5Url = audioList[indexNumbers].h5Url;
    var vm = this
    wx.navigateTo({
      url: "../day_day_listen/details/details?id=" + id + "&season=" + season,
      success: function() {},
      fail: function() {}
    })
  },
})